package com.fanghaoran.view.user.infoFrame;

import com.fanghaoran.controller.InfoController;
import com.fanghaoran.entity.Users;
import com.fanghaoran.view.user.BackGroundPanel;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @Author: win11
 * @Date: 2023/03/26/17:04
 * @Description: 修改密码页面
 */
public class ModifyPasswordFrame extends JFrame {
    public ModifyPasswordFrame(Users user) throws IOException {
        //最外层界面frame的设置
        initFrame();
        //界面中的视图,控件等的设置
        initView(user);

        //设置可见
        this.setVisible(true);
    }

    private void initView(Users user) throws IOException {
        BackGroundPanel panel = new BackGroundPanel(ImageIO.read(new File("src//image//rBackground.jpg")));
        //垂直布局
        Box vBox=Box.createVerticalBox();
        vBox.add(Box.createVerticalStrut(100));
        //水平布局
        Box box1=Box.createHorizontalBox();
        JLabel j1=new JLabel("      原密码:");
        JTextField oriPasswordInput=new JTextField(20);
        box1.add(j1);
        box1.add(Box.createHorizontalStrut(20));
        box1.add(oriPasswordInput);

        Box box2=Box.createHorizontalBox();
        JLabel j2=new JLabel(" 修改后的密码:");
        JTextField modifiedPasswordInput=new JTextField(20);
        box2.add(j2);
        box2.add(Box.createHorizontalStrut(20));
        box2.add(modifiedPasswordInput);

        Box box3=Box.createHorizontalBox();
        JLabel j3=new JLabel("再次输入新密码:");
        JTextField inputAgain=new JTextField(20);
        box3.add(j3);
        box3.add(Box.createHorizontalStrut(20));
        box3.add(inputAgain);

        Box box4=Box.createHorizontalBox();
        JButton btn=new JButton("确认修改");
        box4.add(btn);
        //添加点击事件
        btn.addActionListener(e-> {
                    try {
                        String result = new InfoController().modifyPassword(user,oriPasswordInput.getText(),
                                modifiedPasswordInput.getText(),inputAgain.getText()).getResult();
                        if("原密码为空".equals(result)){
                            JOptionPane.showMessageDialog(panel,"原密码不能为空!");
                        }else if("修改后密码为空".equals(result)){
                            JOptionPane.showMessageDialog(panel,"修改后密码不能为空");
                        }else if("再次输入的密码为空".equals(result)){
                            JOptionPane.showMessageDialog(panel,"再次输入的密码不能为空");
                        }else if("密码不符合规范".equals(result)){
                            JOptionPane.showMessageDialog(panel,"密码不符合规范");
                        }else if("原密码错误".equals(result)){
                            JOptionPane.showMessageDialog(panel,"原密码错误");
                        }else{
                            JOptionPane.showMessageDialog(panel,"修改密码成功!");
                            //关闭当前页面
                            this.dispose();
                        }
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
        );

        vBox.add(box1);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box2);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box3);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box4);

        panel.add(vBox);
        this.add(panel);
    }

    private void initFrame() throws IOException {
        //设置宽高
        this.setSize(500, 500);
        //设置居中
        this.setLocationRelativeTo(null);
        //设置标题
        this.setTitle("扇贝单词");
        //设置标题旁边的logo图标
        this.setIconImage(ImageIO.read(new File("src//image//logo.jpg")));
        //设置置顶
        this.setAlwaysOnTop(true);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
}
